using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Nirvana.AB
{
    public class BuildAssetBundles : MonoBehaviour
    {
        [MenuItem("NirvanaToolkit/Build AssetBundle")]
        private static void Build()
        {
            AssetBundleBuilder builder = new AssetBundleBuilder();
            CollectRootTargets(BundleUtils.AssetsRoot, new List<string>());
            builder.Export();
            //UpdateResourcesFiles(App)
        }

        private static void CollectRootTargets(string dirPath, List<string> excepts)
        {
            var prefabGuids = AssetDatabase.FindAssets("", new[] {dirPath});
            var hashPath = new HashSet<string>();

            foreach (var guid in prefabGuids)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                if (!hashPath.Contains(assetPath))
                {
                    hashPath.Add(assetPath);
                    if (!AssetDatabase.IsValidFolder(assetPath))
                    {
                        if (!excepts.Any(except => assetPath.StartsWith(except)))
                        {
                            var extension = Path.GetExtension(assetPath);
                            if (extension == ".prefab")
                            {
                                // checkMissingScripts
                            }

                            if (extension != ".meta" && extension != ".DS_Store" && extension != ".cs")
                            {
                                var target = AssetBundleUtils.Load(assetPath);
                                if (target != null)
                                {
                                    target.exportAssetType = ExportAssetType.Root;
                                }
                                else
                                {
                                    Debug.LogError("AssetTarget not found path: " + assetPath);
                                }
                            }
                        }
                    }
                }
            }
        }

        private static void UpdateResourcesFiles()
        {
            // ab资源地址
            string abPath = Path.Combine(BundleUtils.AssetsRoot, BundleUtils.GetPlatformName()).ToLower();
            if (!Directory.Exists(abPath))
            {
                Debug.LogError("生成ab资源完成后，在对资源进行热处理");
                return;
            }

            // 平台补丁目录
            string patchPlatformPath = Path.Combine("Patches", BundleUtils.GetPlatformName()).ToLower();
            if (!Directory.Exists(patchPlatformPath))
            {
                Directory.CreateDirectory(patchPlatformPath);
            }

            // app版本对应的资源目录
            string appResourceVersion = "";
            string appResourcePath = Path.Combine(patchPlatformPath, appResourceVersion);
            if (!Directory.Exists(appResourcePath))
            {
                Directory.CreateDirectory(appResourcePath);
            }
            
            // 获取所有大版本目录
            var dirInfo = new DirectoryInfo(patchPlatformPath);
            var dicInfos = dirInfo.GetDirectories();
            foreach (var info in dicInfos)
            {
                string dicName = info.Name;
                if (!Version.TryParse(dicName, out Version version))
                {
                    Debug.LogError($"文件夹格式不对：{dicName}");
                    continue;
                }
                
                UpdateVersionFiles(info.FullName);
            }
        }

        private static void UpdateVersionFiles(string versionPath)
        {
            
        }
    }
}
